package com.lzy.web;

import com.lzy.po.BlogQuery;
import com.lzy.po.Tag;
import com.lzy.service.BlogService;
import com.lzy.service.TagService;
import com.lzy.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Types;

/**
 * @autor shkstart
 * @create 2021-01-04 22:31)
 */
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @RequestMapping("/")
    public String toIndex(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                          Model model){
        model.addAttribute("page", blogService.listBlog(pageable)); //获取分页的blog数据
        model.addAttribute("types", typeService.listType(6)); //获取博客最多的六个分类
        model.addAttribute("tags", tagService.listTag(10)); //获取博客最多的六个分类
        model.addAttribute("recommendBlogs",blogService.listBlogTop(8));//获取最新推荐的8个博客

        return "index";
    }

    @PostMapping("/search")
    public String search(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         Model model,
                         @RequestParam String query){
        model.addAttribute("page",blogService.listBlog(pageable,"%"+query+"%"));
        model.addAttribute("query", query);
        return "search";
    }


    @GetMapping("/blog/{id}")
    public String blog(@PathVariable("id") Long id,Model model){
        model.addAttribute("blog", blogService.getAndConvert(id));
        return "blog";
    }


    //底部数据
    @GetMapping("/footer/newblog")
    public String newBlogs(Model model){
        model.addAttribute("newblogs", blogService.listBlogTop(3));
        return "_fragments :: newblogList";
    }

}
